#include "ui_menu.h"
#include "ui_index.h"

MENU_PARAS_NODE *headnode = NULL;
MENU_PARAS_NODE *curnode = NULL;
MENU_PARAS_NODE curnode_t = {0};

void Char_Show(char chr) {
	printf("%c", chr);
}

void Item_Show(char *title, int select, int position) {
	if (TRUE == select) {
		Char_Show('>');
	} else if (FALSE == select) {
		Char_Show(' ');
	}
	printf("%s\n", title);
}

MENU_PARAS_NODE *Add_Down_Page(void *param, int *_default, MENU_PARAS_NODE *last_node, char *last_title, char *cur_title, CB_MENU cb_show) {	

	MENU_PARAS_NODE *pnode = NULL;
	MENU_PARAS_NODE *phead = NULL;
	MENU_PARAS_NODE *newnode = NULL;
	int next = 0;
	
	if (NULL == headnode) {
		headnode = (MENU_PARAS_NODE *)malloc(sizeof(MENU_PARAS_NODE));
		if (NULL == headnode) {
			printf("创建链表头失败\n");
			return NULL;
		}
		memset(headnode, 0, sizeof(MENU_PARAS_NODE));
		headnode->param = param;
		headnode->_default = _default;
		if (NULL != last_title) {
			strcpy(headnode->last_title, last_title);
		}
		if (NULL != cur_title) {
			strcpy(headnode->cur_title, cur_title);
		}
		headnode->cb_show = cb_show;
		headnode->down = headnode;
		curnode = headnode;
		return headnode;
	}

	if (NULL != last_node) {
		pnode = last_node;
		if (NULL == pnode->next) {
			next = 1;
		} else {
			phead = pnode = pnode->next;
			while (phead != pnode->down) {
				pnode = pnode->down;
			} 
		}
	} else {
		phead = pnode = headnode;
		while (phead != pnode->down) {
			pnode = pnode->down;
		}
	}

	newnode = (MENU_PARAS_NODE *)malloc(sizeof(MENU_PARAS_NODE));
	if (NULL == newnode) {
		printf("新建链表头失败\n");
		return NULL;
	}
	memset(newnode, 0, sizeof(MENU_PARAS_NODE));
	newnode->param = param;
	newnode->_default = _default;
	if (NULL != last_title) {
		strcpy(newnode->last_title, last_title);
	}
	if (NULL != cur_title) {
		strcpy(newnode->cur_title, cur_title);
	}
	newnode->cb_show = cb_show;
	if (next) {
		pnode->next = newnode;
		newnode->prev = pnode;
		newnode->up = newnode;
		newnode->down = newnode;
		newnode->left = newnode;
		newnode->right = newnode;
		printf("Next:%s %s %p %p\n", last_title, cur_title, pnode, newnode);		
	} else {
		pnode->down = newnode;
		newnode->up = pnode;
		if (NULL != last_node) {
			if (NULL != pnode->prev) {
				newnode->down = pnode->prev->next;
				pnode->prev->next->up = newnode;
			}
		}else {
			newnode->down = headnode;
			headnode->up = newnode;
		}
		newnode->prev = pnode->prev;
		printf("Down:%s %s %p %p\n", last_title, cur_title, pnode, newnode);		
	}  
	
	return newnode;
}

MENU_PARAS_NODE *Add_Page_Exec(void *param, int *_default, MENU_PARAS_NODE *last_node, char *last_title, char *cur_title, CB_MENU cb_exec) {
	MENU_PARAS_NODE *rnode1 = Add_Down_Page(param, _default, last_node, last_title, cur_title, NULL);
	MENU_PARAS_NODE *rnode2 = Add_Down_Page(param, _default, rnode1, last_title, cur_title, NULL);
	rnode2->cb_exec = cb_exec;
	return rnode2;
}

MENU_PARAS_NODE *Delete_This_Page(MENU_PARAS_NODE *pnode) {

	if (pnode->down) {
		pnode->up->down = pnode->down;
	}
	if (pnode->down) {
		pnode->down->up = pnode->up;
	}
	memcpy(&curnode_t, pnode, sizeof(MENU_PARAS_NODE));
	free(pnode);	
	memset(pnode, 0, sizeof(MENU_PARAS_NODE));
	*pnode = curnode_t;
	return NULL;
}

MENU_PARAS_NODE *Menu_Prev(void) {
	if (NULL == curnode) {
		return NULL;
	}

	if (NULL != curnode->prev) {
		return curnode = curnode->prev;
	} 

	return NULL;
}

MENU_PARAS_NODE *Menu_Next(void) {	
	if (NULL == curnode) {
		return NULL;
	}

	if (NULL != curnode->next) {
		return curnode = curnode->next;
	} 

	return NULL;
}

MENU_PARAS_NODE *Menu_Up(void) {
	if (NULL == curnode) {
		return NULL;
	}

	if (NULL != curnode->up) {
		return curnode = curnode->up;
	} 

	return NULL;
}

MENU_PARAS_NODE *Menu_Down(void) {
	if (NULL == curnode) {
		return NULL;
	}

	if (NULL != curnode->down) {
		return curnode = curnode->down;
	} 

	return NULL;
}

MENU_PARAS_NODE *Menu_Left(void) {
	if (NULL == curnode) {
		return NULL;
	}

	if (NULL != curnode->left) {
		return curnode = curnode->left;
	} 

	return NULL;
}

MENU_PARAS_NODE *Menu_Right(void) {
	if (NULL == curnode) {
		return NULL;
	}

	if (NULL != curnode->right) {
		return curnode = curnode->right;
	} 

	return NULL;
}

void Menu_Handle() {

	char ch = 0;
	int cnt = 0;
	int select = 0, position = 0;
	int paras = 0, *_default = NULL;
	MENU_PARAS_NODE *ret = NULL;
	MENU_PARAS_NODE cmp = {0};
	MENU_PARAS_NODE *pnode = NULL;
	MENU_PARAS_NODE *phead = NULL;
	
	scanf("%c", &ch);
	
	if ('e' == ch) {
		ret = Menu_Next();	
	} else if ('q' == ch) {
		ret = Menu_Prev();
	} else if ('w' == ch) {
		ret = Menu_Up();
	} else if ('z' == ch) {
		ret = Menu_Down();
	} else if ('a' == ch) {
		ret = Menu_Left();
	} else if ('d' == ch) {
		ret = Menu_Right();
	}

	if (NULL == ret) {
		return;
	}

	pnode = ret;
	if (NULL != pnode->cb_exec) {
		pnode->cb_exec(pnode, ch);
		return;
	}
	pnode = pnode->prev;
	if (NULL == pnode) {
		pnode = headnode;
	} else {
		pnode = pnode->next;
	}
	phead = pnode;
	while (NULL != pnode) {
		if (!strcmp(ret->cur_title, pnode->cur_title)) {
			break;
		}
		pnode = pnode->down;
		cnt++;
		if (phead == pnode) break;
	}
	pnode = ret;
	_default = pnode->_default;
	pnode = pnode->prev;
	if (NULL == pnode) {
		pnode = headnode;
	} else {
		pnode = pnode->next;
	}
	phead = pnode;
	if ((PAGE_ITEM_NUM_MAX - 1) <= cnt) {
		cnt -= (PAGE_ITEM_NUM_MAX - 1);
		while (cnt && NULL != pnode) {
			pnode = pnode->down;
			cnt--;
			if (phead == pnode) break;
		}
	}
	cnt = 0;
	while (NULL != pnode) {
		if (0 != strlen(pnode->cur_title)) {
			if ('e' != ch) {
				if (!strcmp(pnode->cur_title, ret->cur_title)) {
					select = TRUE;
				} else {
					select = FALSE;
				}
			} else {
				if (paras == *_default) {
					select = TRUE;
					phead = curnode;
					while (NULL != curnode) {
						if (!strcmp(curnode->cur_title, pnode->cur_title)) {
							break;
						}
						curnode = curnode->down;
						if (phead == curnode) break;
					} 
				} else {
					select = FALSE;
				}
				paras++;
			}
			Item_Show(pnode->cur_title, select, position);
		}
		pnode = pnode->down;
		if (++cnt >= PAGE_ITEM_NUM_MAX) {
			break;
		}
		if (phead == pnode) break;
	}
	
	pnode = ret;
	if (NULL != pnode->cb_show) {
		pnode->cb_show(pnode, ch);
	}
}

void Home_Page_Show(MENU_PARAS_NODE *param) {
	
	int cnt = 0;
	int select = 0, position = 0;
	MENU_PARAS_NODE *pnode = headnode;
	MENU_PARAS_NODE *phead = NULL;
	
	phead = pnode;
	while (NULL != pnode) {
		if (0 != strlen(pnode->cur_title)) {
			if (!strcmp(pnode->cur_title, param->cur_title)) {
				select = TRUE;
			} else {
				select = FALSE;
			}
			Item_Show(pnode->cur_title, select, position);
		}
		pnode = pnode->down;
		if (++cnt >= PAGE_ITEM_NUM_MAX) {
			return;
		}
		if (phead == pnode) break;
	}
}

